package com.ace.user.api.restful;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ace.user.common.bean.RequestDTO;
import com.ace.user.common.bean.ResponseDTO;
import com.ace.user.common.bean.dto.UserDTO;
import com.ace.user.common.constant.Group;
import com.ace.user.common.constant.R;
import com.ace.user.module.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author weihl
 * @date 2019/10/11 14:48
 */
@RestController
@RequestMapping("api/v1/user")
@Slf4j
public class LoginController {
    @Autowired
    private IUserService userService;

    /**
     * 用户登录
     * @param requestDTO
     * @return
     */
    @PostMapping("/login")
    public Map<String,Object> login(@RequestBody @Validated({Group.Login.class})
                                     RequestDTO<UserDTO> requestDTO){
        Map<String,Object> resultMap = userService.login(requestDTO);
        return resultMap;
    }


    /**
     * 用户登出
     * @param requestDTO
     * @return
     */
    @PostMapping("/logout")
    public JSONObject logout(@RequestBody @Validated({Group.Logout.class}) RequestDTO<UserDTO> requestDTO){
        userService.logout(requestDTO);
        JSONObject data = new JSONObject();
        data.put("ret", true);
        return data;
    }
}
